package com.cybertron.am.wordpress.util;

import com.ne.boot.common.exception.NEException;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * Created by xiezhouyan on 16-10-12.
 */
@Slf4j
public class ProxyUtil {

    public static Proxy createProxy(ProxyInfo proxyInfo) {
        Proxy.Type type = Proxy.Type.HTTP;
        if (ProxyInfo.Protocol.SOCKS == proxyInfo.getProtocol()) {
            type = Proxy.Type.SOCKS;
        }
        Proxy proxy = new Proxy(type, new InetSocketAddress(proxyInfo.getIp(), proxyInfo.getPort()));
        return proxy;
    }

    public static ProxyInfo createProxyInfo(String proxy) {
        try {
            URI uri = new URI(proxy);
            ProxyInfo proxyInfo = new ProxyInfo(uri.getHost(), uri.getPort(), ProxyInfo.Protocol.val(uri.getScheme()));
            return proxyInfo;
        } catch (URISyntaxException e) {
            throw new NEException("parse uri error " + proxy);
        }
    }
}
